package telas;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import arquivo.Arquivo;
import javax.swing.LayoutStyle.ComponentPlacement;

public class ArquivoTelaPrincipal extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JButton btnLeArquivo;
	private JScrollPane scrollPane;
	private JTextArea taTexto;
	private JButton btnGravaArquivo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ArquivoTelaPrincipal frame = new ArquivoTelaPrincipal();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		Arquivo arq = new Arquivo();
		
		Thread t01 = new Thread(arq);
		
		t01.start();
		
	}

	/**
	 * Create the frame.
	 */
	public ArquivoTelaPrincipal() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(25)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(getBtnLeArquivo())
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(getBtnGravaArquivo()))
						.addComponent(getScrollPane(), GroupLayout.PREFERRED_SIZE, 361, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(46, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(getScrollPane(), GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(getBtnLeArquivo())
						.addComponent(getBtnGravaArquivo()))
					.addContainerGap(52, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}

	private JButton getBtnLeArquivo() {
		if (btnLeArquivo == null) {
			btnLeArquivo = new JButton("Le Arquivo");
			btnLeArquivo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

					getTaTexto().setText("");
					Arquivo arquivo = new Arquivo();
					BufferedReader br = arquivo.abreArquivoLeitura("c:\\ler.txt");
				
					taTexto.append(arquivo.leConteudoArquivo().concat("\n"));
					
					arquivo.fechaArquivo();
				}
			});
		}
		return btnLeArquivo;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTaTexto());
		}
		return scrollPane;
	}

	private JTextArea getTaTexto() {
		if (taTexto == null) {
			taTexto = new JTextArea();
			taTexto.setLineWrap(true);
		}
		return taTexto;
	}
	private JButton getBtnGravaArquivo() {
		if (btnGravaArquivo == null) {
			btnGravaArquivo = new JButton("Grava Arquivo");
			btnGravaArquivo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					
					Arquivo arquivo = new Arquivo();
					
					arquivo.abreArquivoGravacaoString("c:\\ler.txt", taTexto.getText());
					
					arquivo.fechaArquivo();
					
					getTaTexto().setText("");
				}
			});
		}
		return btnGravaArquivo;
	}
}
